package timesheet.repository;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import timesheet.model.Event;
import timesheet.model.EventTime;
import timesheet.model.Project;
import timesheet.model.Users;

@Repository
public class EventTimeRepository {
	@PersistenceContext
    EntityManager em;
	
	
	public void createEvent(EventTime e) {
		em.persist(e);
	}
	
	
	public void merge(EventTime e) {
		em.merge(e);
	}
	
	public void detacheEntity(EventTime e) {
		em.detach(e);
	}
	
	public void remove(EventTime e) {
		em.remove(e);
	}
	
	public EventTime find(Long id){
		return em.find(EventTime.class, id);
	}
	
	public List<EventTime> getAllBadging() {
		List<EventTime> eventTimeList = (List<EventTime>)em.createQuery("select e from EventTime e ORDER BY e.start")
         .getResultList();
    return eventTimeList;     
	}
	
	public List<EventTime> getAllTimeByEvent(Event e){
		List<EventTime> eventTimeList = (List<EventTime>) em.createQuery("select e from EventTime e where e.e = :e").setParameter("e", e).getResultList();
		return eventTimeList;
	}
	
	
	public EventTime getLastEventTimeByEvent(Event e) {
//		List<Badging> badgingList = em.createQuery("select MAX(b.id) from Badging b where b.u = :u")
//		 .setParameter("u", u);
//         .getSingleOrNullResult(query);
			 Query query = em.createQuery("select MAX(e.id) from EventTime e where e.e = :e").setParameter("e", e);
	         Long id = getSingleOrNullResult(query);
	         if(id!=null){
	        	 EventTime et = em.find(EventTime.class, id);
		         return et;   
	         }
	         else{return null;}
	}
	
//	public List<Event> getUserEvent(Long section){
//		String user = String.valueOf(section);
//		List<Event> EventList = em.createQuery("select e from Event e where e.sectionId = :user ORDER BY e.startTime")
//				  				  .setParameter("user", user)
//				  				  .getResultList();
//		return EventList;
//	}
	
	public List<EventTime> getAllEventTimeByDate(Date from, Date to, String usercondition, String projectCondition){
		List <EventTime> list = em.createQuery("select e from EventTime e where e.start >= :from and e.start <= :to "+usercondition+" "+projectCondition+" ORDER BY e.e.id").setParameter("from", from).setParameter("to", to).getResultList();
		return list;
	}
	
	public List<EventTime> getUserByProject(Project p){
		List <EventTime> list = em.createQuery("select e from EventTime e where e.e.p = :p ORDER BY e.e.id").setParameter("p", p).getResultList();
		return list;
	}
	
	public List<Users> getUserNameByProject(Project p){
		List <Users> list = em.createQuery("select distinct e.e.u from EventTime e where e.e.p = :p").setParameter("p", p).getResultList();
		return list;
	}
	
	protected Long getSingleOrNullResult(Query query) {
        try {
            Long result = (Long)query.getSingleResult();
            return result;
        } catch (NoResultException nre) {
            return null;  // Normal, entity is just not found.
        }
    }
	
	public List<EventTime> getEventTimeByUser(Users u){
		List <EventTime> list = em.createQuery("select e from EventTime e where e.e.u = :u").setParameter("u", u).getResultList();
		return list;
	}
}
